package com.zq.notificationdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mShowBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showNotification();

        mShowBtn = findViewById(R.id.show_notification);
        mShowBtn.setOnClickListener(this);
    }


    /**
     * 由于我们只需要在android8.0以上创建，所以在代码中进行判断：
     * 这里封装了一个方法，如果api大于android8.0就创建渠道返回渠道id，否则返回空。这里我只传了三个必要的参数，还有其他的设置在通知渠道那一节单独介绍。
     *
     * @param channelID
     * @param channelNAME
     * @param level
     * @return
     */
    private String createNotificationChannel(String channelID, String channelNAME, int level) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
            manager.createNotificationChannel(channel);
            return channelID;
        } else {
            return null;
        }
    }

    private void showNotification() {
        showNotification("通知", "收到一条消息", "my_channel_ID", "my_channel_NAME");
    }


    private void showNotification(String title, String content) {
        showNotification(title, content, "my_channel_ID", "my_channel_NAME");
    }

    private void showNotification(String title, String content, String channelId, String channelName) {
        Intent intent = new Intent(this, MainActivity2.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        String channelId1 = createNotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, channelId)
                .setContentTitle(title)
                .setContentText(content)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setChannelId(channelId1)
                .setAutoCancel(true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(100, notification.build());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.show_notification:
                showNotification("测试", "这是第二条消息");
                break;
        }
    }
}
